﻿using DataAccess.ConsumerInstallmentLoan;
using FOModels.ConsumerInstallmentLoan;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Bussiness.ConsumerInstallmentLoan
{
    public class ConsumerInstallmentLoanBLL
    {
        private ConsumerInstallmentLoanDAL dal;
        public ConsumerInstallmentLoanBLL()
        {
            dal = new ConsumerInstallmentLoanDAL();
        }
        public List<ConsumerInstallmentLoanApplyModels> getcontract()
        {
            List<ConsumerInstallmentLoanApplyModels> list = new List<ConsumerInstallmentLoanApplyModels>();
            ConsumerInstallmentLoanApplyModels fur;
            DataTable tb = dal.getcontract();
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new ConsumerInstallmentLoanApplyModels();
                    fur.ConsumerInstallmentLoanApplyID = int.Parse(row[0].ToString());
                    fur.ConsumerInstallmentLoanID = int.Parse(row[1].ToString());
                    fur.CustomerName = row[2].ToString();
                    fur.CustomerEmail = row[3].ToString();
                    fur.CustomerPhone = row[4].ToString();
                    fur.CustomerIncome = row[5].ToString();
                    fur.CustomerDOB = row[6].ToString();
                    fur.StatusID = int.Parse(row[7].ToString());
                    fur.AppDate = row[8].ToString();
                    fur.ConsumerInstallmentLoanName = row[10].ToString();
                    fur.StatusName = row[17].ToString();
                    fur.CompanyImg = row[19].ToString();
                    fur.CompanyName = row[26].ToString();
                    list.Add(fur);
                }
            }
            return list;
        }
        public List<ConsumerInstallmentLoanApplyModels> getcontractbystatuscount(int statusid)
        {
            List<ConsumerInstallmentLoanApplyModels> list = new List<ConsumerInstallmentLoanApplyModels>();
            ConsumerInstallmentLoanApplyModels fur;
            DataTable tb = dal.getcontractbystatuscount(statusid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new ConsumerInstallmentLoanApplyModels();
                    fur.ConsumerInstallmentLoanApplyID = int.Parse(row[0].ToString());
                    fur.ConsumerInstallmentLoanID = int.Parse(row[1].ToString());
                    fur.CustomerName = row[2].ToString();
                    fur.CustomerEmail = row[3].ToString();
                    fur.CustomerPhone = row[4].ToString();
                    fur.CustomerIncome = row[5].ToString();
                    fur.CustomerDOB = row[6].ToString();
                    fur.StatusID = int.Parse(row[7].ToString());
                    fur.AppDate = row[8].ToString();
                    fur.ConsumerInstallmentLoanName = row[10].ToString();
                    fur.StatusName = row[17].ToString();
                    fur.CompanyImg = row[19].ToString();
                    fur.CompanyName = row[26].ToString();
                    list.Add(fur);
                }
            }
            return list;
        }
        public ConsumerInstallmentLoanApplyModels getcontractbyid(int ConsumerInstallmentLoanapplyid)
        {

            ConsumerInstallmentLoanApplyModels fur = new ConsumerInstallmentLoanApplyModels();
            DataTable tb = dal.getcontractbyid(ConsumerInstallmentLoanapplyid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new ConsumerInstallmentLoanApplyModels();
                    fur.ConsumerInstallmentLoanApplyID = int.Parse(row[0].ToString());
                    fur.ConsumerInstallmentLoanID = int.Parse(row[1].ToString());
                    fur.CustomerName = row[2].ToString();
                    fur.CustomerEmail = row[3].ToString();
                    fur.CustomerPhone = row[4].ToString();
                    fur.CustomerIncome = row[5].ToString();
                    fur.CustomerDOB = row[6].ToString();
                    fur.StatusID = int.Parse(row[7].ToString());
                    fur.AppDate = row[8].ToString();
                    fur.ConsumerInstallmentLoanName = row[10].ToString();
                    fur.StatusName = row[17].ToString();
                    fur.CompanyImg = row[19].ToString();
                    fur.CompanyName = row[26].ToString();

                }
            }
            return fur;
        }
        public int ConsumerInstallmentLoanApply(int ConsumerInstallmentLoanID, ConsumerInstallmentLoanApplyModels ConsumerInstallmentLoan)
        {
            if (dal.ConsumerInstallmentLoanApply(ConsumerInstallmentLoanID, ConsumerInstallmentLoan) > 0)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
        public int DeleteConsumerInstallmentLoanApply(int ConsumerInstallmentLoanApplyID)
        {
            if (dal.DeleteConsumerInstallmentLoanApply(ConsumerInstallmentLoanApplyID) > 0)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
        public int UpdateStatus(ConsumerInstallmentLoanApplyModels ConsumerInstallmentLoan)
        {
            if (dal.UpdateStatus(ConsumerInstallmentLoan) > 0)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
        public ConsumerInstallmentLoanModels getTOP1()
        {

            ConsumerInstallmentLoanModels fur = new ConsumerInstallmentLoanModels();
            DataTable tb = dal.getTOP1();
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {

                    fur.ConsumerInstallmentLoanID = int.Parse(row[0].ToString());
                }
                return fur;
            }
            else
                return null;
        }
        public List<ConsumerInstallmentLoanModels> getall(int langid)
        {
            List<ConsumerInstallmentLoanModels> list = new List<ConsumerInstallmentLoanModels>();
            ConsumerInstallmentLoanModels fur;
            DataTable tb = dal.getall(langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {

                    fur = new ConsumerInstallmentLoanModels();
                    fur.ConsumerInstallmentLoanID = int.Parse(row[0].ToString());
                    fur.ConsumerInstallmentLoanName = row[9].ToString();
                    fur.CompanyID = int.Parse(row[2].ToString());
                    fur.Benefit = WebUtility.HtmlDecode(row[10].ToString());
                    fur.MinimumBalance = row[3].ToString();
                    fur.MaximumBalance = row[4].ToString();
                    fur.HomeLoanPlanID = int.Parse(row[5].ToString());
                    fur.Description = WebUtility.HtmlDecode(row[11].ToString());
                    fur.FeeContent = WebUtility.HtmlDecode(row[12].ToString());
                    fur.MoreInfo = WebUtility.HtmlDecode(row[13].ToString());
                    fur.Display = bool.Parse(row[6].ToString());
                    fur.CompanyName = row[22].ToString();
                    fur.CompanyImg = row[15].ToString();
                    fur.FlagImage = row[27].ToString();
                    list.Add(fur);
                }
            }
            return list;
        }
        public ConsumerInstallmentLoanModels getConsumerInstallmentLoanByID(int ConsumerInstallmentLoanID, int langid)
        {

            ConsumerInstallmentLoanModels fur = new ConsumerInstallmentLoanModels();
            DataTable tb = dal.getConsumerInstallmentLoanByID(ConsumerInstallmentLoanID, langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur.ConsumerInstallmentLoanID = int.Parse(row[0].ToString());
                    fur.ConsumerInstallmentLoanName = row[9].ToString();
                    fur.CompanyID = int.Parse(row[2].ToString());
                    fur.Benefit = WebUtility.HtmlDecode(row[10].ToString());
                    fur.MinimumBalance = String.Format("{0:0,0}", double.Parse(row[3].ToString())).Replace(',', '.');
                    fur.MaximumBalance = String.Format("{0:0,0}", double.Parse(row[4].ToString())).Replace(',', '.');
                    fur.HomeLoanPlanID = int.Parse(row[5].ToString());
                    fur.Description = WebUtility.HtmlDecode(row[11].ToString());
                    fur.FeeContent = WebUtility.HtmlDecode(row[12].ToString());
                    fur.MoreInfo = WebUtility.HtmlDecode(row[13].ToString());
                    fur.Display = bool.Parse(row[6].ToString());
                    fur.CompanyName = row[22].ToString();
                    fur.CompanyImg = row[15].ToString();
                }
                return fur;
            }
            else
                return null;
        }
        public ConsumerInstallmentLoanModels getConsumerInstallmentLoanByIDManager(int ConsumerInstallmentLoanID, int langid)
        {

            ConsumerInstallmentLoanModels fur = new ConsumerInstallmentLoanModels();
            DataTable tb = dal.getConsumerInstallmentLoanByIDManager(ConsumerInstallmentLoanID, langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur.ConsumerInstallmentLoanID = int.Parse(row[0].ToString());
                    fur.ConsumerInstallmentLoanName = row[9].ToString();
                    fur.CompanyID = int.Parse(row[2].ToString());
                    fur.Benefit = WebUtility.HtmlDecode(row[10].ToString());
                    fur.MinimumBalance = String.Format("{0:0,0}", double.Parse(row[3].ToString())).Replace(',', '.');
                    fur.MaximumBalance = String.Format("{0:0,0}", double.Parse(row[4].ToString())).Replace(',', '.');
                    fur.HomeLoanPlanID = int.Parse(row[5].ToString());
                    fur.Description = WebUtility.HtmlDecode(row[11].ToString());
                    fur.FeeContent = WebUtility.HtmlDecode(row[12].ToString());
                    fur.MoreInfo = WebUtility.HtmlDecode(row[13].ToString());
                    fur.Display = bool.Parse(row[6].ToString());
                    fur.CompanyName = row[22].ToString();
                    fur.CompanyImg = row[15].ToString();
                }
                return fur;
            }
            else
                return null;
        }
        public List<ConsumerInstallmentLoanModels> getConsumerInstallmentLoanDefault(int langid)
        {
            List<ConsumerInstallmentLoanModels> list = new List<ConsumerInstallmentLoanModels>();
            ConsumerInstallmentLoanModels fur;
            DataTable tb = dal.getConsumerInstallmentLoanDefault(langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new ConsumerInstallmentLoanModels();
                    fur.ConsumerInstallmentLoanID = int.Parse(row[0].ToString());
                    fur.ConsumerInstallmentLoanName = row[9].ToString();
                    fur.CompanyID = int.Parse(row[2].ToString());
                    fur.Benefit = WebUtility.HtmlDecode(row[10].ToString());
                    fur.MinimumBalance = String.Format("{0:0,0}", double.Parse(row[3].ToString())).Replace(',', '.');
                    fur.MaximumBalance = String.Format("{0:0,0}", double.Parse(row[4].ToString())).Replace(',', '.');
                    fur.HomeLoanPlanID = int.Parse(row[5].ToString());
                    fur.Description = WebUtility.HtmlDecode(row[11].ToString());
                    fur.FeeContent = WebUtility.HtmlDecode(row[12].ToString());
                    fur.MoreInfo = WebUtility.HtmlDecode(row[13].ToString());
                    fur.Display = bool.Parse(row[6].ToString());
                    fur.CompanyName = row[22].ToString();
                    fur.CompanyImg = row[15].ToString();
                    fur.InterestVND = float.Parse(row[26].ToString());
                    list.Add(fur);
                }
            }
            return list;
        }
        public List<ConsumerInstallmentLoanModels> getConsumerInstallmentLoanrelati(int CompanyID, int HomeLoanID, int langid)
        {
            List<ConsumerInstallmentLoanModels> list = new List<ConsumerInstallmentLoanModels>();
            ConsumerInstallmentLoanModels fur;
            DataTable tb = dal.getConsumerInstallmentLoanrelati(CompanyID, HomeLoanID, langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new ConsumerInstallmentLoanModels();
                    fur.ConsumerInstallmentLoanID = int.Parse(row[0].ToString());
                    fur.ConsumerInstallmentLoanName = row[9].ToString();
                    fur.CompanyID = int.Parse(row[2].ToString());
                    fur.Benefit = WebUtility.HtmlDecode(row[10].ToString());
                    fur.MinimumBalance = String.Format("{0:0,0}", double.Parse(row[3].ToString())).Replace(',', '.');
                    fur.MaximumBalance = String.Format("{0:0,0}", double.Parse(row[4].ToString())).Replace(',', '.');
                    fur.HomeLoanPlanID = int.Parse(row[5].ToString());
                    fur.Description = WebUtility.HtmlDecode(row[11].ToString());
                    fur.FeeContent = WebUtility.HtmlDecode(row[12].ToString());
                    fur.MoreInfo = WebUtility.HtmlDecode(row[13].ToString());
                    fur.Display = bool.Parse(row[6].ToString());
                    fur.CompanyName = row[22].ToString();
                    fur.CompanyImg = row[15].ToString();
                    list.Add(fur);
                }
            }
            return list;
        }
        public List<ConsumerInstallmentLoanModels> getConsumerInstallmentLoan(int planid, string balance, int years, int langid)
        {
            List<ConsumerInstallmentLoanModels> list = new List<ConsumerInstallmentLoanModels>();
            ConsumerInstallmentLoanModels fur;
            DataTable tb = dal.getConsumerInstallmentLoan(planid, balance, years, langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new ConsumerInstallmentLoanModels();
                    fur.ConsumerInstallmentLoanID = int.Parse(row[0].ToString());
                    fur.ConsumerInstallmentLoanName = row[9].ToString();
                    fur.CompanyID = int.Parse(row[2].ToString());
                    fur.Benefit = WebUtility.HtmlDecode(row[10].ToString());
                    fur.MinimumBalance = String.Format("{0:0,0}", double.Parse(row[3].ToString())).Replace(',', '.');
                    fur.MaximumBalance = String.Format("{0:0,0}", double.Parse(row[4].ToString())).Replace(',', '.');
                    fur.HomeLoanPlanID = int.Parse(row[5].ToString());
                    fur.Description = WebUtility.HtmlDecode(row[11].ToString());
                    fur.FeeContent = WebUtility.HtmlDecode(row[12].ToString());
                    fur.MoreInfo = WebUtility.HtmlDecode(row[13].ToString());
                    fur.Display = bool.Parse(row[6].ToString());
                    fur.CompanyName = row[22].ToString();
                    fur.CompanyImg = row[15].ToString();
                    fur.InterestVND = float.Parse(row[26].ToString());
                    list.Add(fur);
                }
            }
            return list;
        }
        public List<ConsumerInstallmentLoanModels> getConsumerInstallmentLoanByProvider(int planid, string balance, int years, int CompanyID, int langid)
        {
            List<ConsumerInstallmentLoanModels> list = new List<ConsumerInstallmentLoanModels>();
            ConsumerInstallmentLoanModels fur;
            DataTable tb = dal.getConsumerInstallmentLoanByProvider(planid, balance, years, CompanyID, langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new ConsumerInstallmentLoanModels();
                    fur.ConsumerInstallmentLoanID = int.Parse(row[0].ToString());
                    fur.ConsumerInstallmentLoanName = row[9].ToString();
                    fur.CompanyID = int.Parse(row[2].ToString());
                    fur.Benefit = WebUtility.HtmlDecode(row[10].ToString());
                    fur.MinimumBalance = String.Format("{0:0,0}", double.Parse(row[3].ToString())).Replace(',', '.');
                    fur.MaximumBalance = String.Format("{0:0,0}", double.Parse(row[4].ToString())).Replace(',', '.');
                    fur.HomeLoanPlanID = int.Parse(row[5].ToString());
                    fur.Description = WebUtility.HtmlDecode(row[11].ToString());
                    fur.FeeContent = WebUtility.HtmlDecode(row[12].ToString());
                    fur.MoreInfo = WebUtility.HtmlDecode(row[13].ToString());
                    fur.Display = bool.Parse(row[6].ToString());
                    fur.CompanyName = row[22].ToString();
                    fur.CompanyImg = row[15].ToString();
                    fur.InterestVND = float.Parse(row[26].ToString());
                    list.Add(fur);
                }
            }
            return list;
        }
        public List<ConsumerInstallmentLoanModels> getConsumerInstallmentLoanDefaultByProvider(int CompanyID, int langid)
        {
            List<ConsumerInstallmentLoanModels> list = new List<ConsumerInstallmentLoanModels>();
            ConsumerInstallmentLoanModels fur;
            DataTable tb = dal.getConsumerInstallmentLoanDefaultByProvider(CompanyID, langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new ConsumerInstallmentLoanModels();
                    fur.ConsumerInstallmentLoanID = int.Parse(row[0].ToString());
                    fur.ConsumerInstallmentLoanName = row[9].ToString();
                    fur.CompanyID = int.Parse(row[2].ToString());
                    fur.Benefit = WebUtility.HtmlDecode(row[10].ToString());
                    fur.MinimumBalance = String.Format("{0:0,0}", double.Parse(row[3].ToString())).Replace(',', '.');
                    fur.MaximumBalance = String.Format("{0:0,0}", double.Parse(row[4].ToString())).Replace(',', '.');
                    fur.HomeLoanPlanID = int.Parse(row[5].ToString());
                    fur.Description = WebUtility.HtmlDecode(row[11].ToString());
                    fur.FeeContent = WebUtility.HtmlDecode(row[12].ToString());
                    fur.MoreInfo = WebUtility.HtmlDecode(row[13].ToString());
                    fur.Display = bool.Parse(row[6].ToString());
                    fur.CompanyName = row[22].ToString();
                    fur.CompanyImg = row[15].ToString();
                    fur.InterestVND = float.Parse(row[26].ToString());
                    list.Add(fur);
                }
            }
            return list;
        }
        public int UpdateConsumerInstallmentLoan(ConsumerInstallmentLoanModels models, int langid)
        {
            if (dal.UpdateConsumerInstallmentLoan(models) > 0 && dal.UpdateConsumerInstallmentLoanLang(models, langid) > 0)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
        public int InsertConsumerInstallmentLoan(ConsumerInstallmentLoanModels models)
        {
            if (dal.InsertConsumerInstallmentLoan(models) > 0)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
        public int InsertConsumerInstallmentLoanLang(ConsumerInstallmentLoanModels models, int langid)
        {
            if (dal.InsertConsumerInstallmentLoanLang(models, langid) > 0)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
        public int DeleteConsumerInstallmentLoan(int CarLoanID)
        {
            if (dal.DeleteConsumerInstallmentLoanLang(CarLoanID) > 0 && dal.DeleteConsumerInstallmentLoan(CarLoanID) > 0)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
    }
}
